/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Model.Contact;
import adressbook.AdressBook;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

/**
 *
 * @author Martin LEGRIS
 */
public class NewContactWindow extends JFrame implements ActionListener {

    private JPanel container = new JPanel();
    private JPanel buttons_pan = new JPanel();
    private JPanel form = new JPanel();
    private JTextField fname = new JTextField();
    private JTextField lname = new JTextField();
    private JTextField haddress = new JTextField();
    private JTextField caddress = new JTextField();
    private JTextField mphone = new JTextField();
    private JTextField hphone = new JTextField();
    private JTextField prof_mobile_phone = new JTextField();
    private JTextField prof_office_phone = new JTextField();
    private JTextField home_page = new JTextField();
    private JTextField prof_email = new JTextField();
    private JTextField perso_email = new JTextField();
    private JTextField skype = new JTextField();
    private JTextField notes = new JTextField();
    private JButton addContact = new JButton();
    private JButton cancel = new JButton();
    private JButton selectPicture = new JButton();
    private JLabel test = new JLabel();
    String data[] = new String[14];
    final JFileChooser fc = new JFileChooser();
    BufferedImage img = null;
    JLabel imgLabel = new JLabel();

    public NewContactWindow() throws IOException {
        //Create and set up the window.
        this.setTitle("New Contact");
        this.setSize(400, 200);
        this.setLocationRelativeTo(null); //on positionne la fenetre au centre
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setContentPane(container);

        //JPanel buttons
        buttons_pan.setLayout(new GridLayout(4, 1));
        container.add(buttons_pan);

        //JPanel form
        GridLayout layout = new GridLayout(13, 2);
        layout.setHgap(5);
        layout.setVgap(5);
        form.setLayout(layout);
        container.add(form);

        //Picture
        try {
            img = ImageIO.read(new File("avatar-vide.jpeg"));
        } catch (IOException e) {
        }
        imgLabel.setIcon(new ImageIcon(img));
        buttons_pan.add(imgLabel);

        //JButton
        addContact.setText("Add");
        buttons_pan.add(addContact);
        addContact.addActionListener(this);

        cancel.setText("Cancel");
        buttons_pan.add(cancel);
        cancel.addActionListener(this);

        selectPicture.setText("Select a picture");
        buttons_pan.add(selectPicture);
        selectPicture.addActionListener(this);

        //JLabel
        //form
        JLabel lfname = new JLabel(AdressBook.FIRSTNAME);
        form.add(lfname);
        form.add(fname);

        JLabel llname = new JLabel(AdressBook.LASTNAME);
        form.add(llname);
        form.add(lname);

        JLabel lhaddress = new JLabel(AdressBook.HOME_ADDRRESS);
        form.add(lhaddress);
        form.add(haddress);

        JLabel lcaddress = new JLabel(AdressBook.COMPANY_ADDRESS);
        form.add(lcaddress);
        form.add(caddress);

        JLabel lmphone = new JLabel(AdressBook.PERSO_MOBILE_PHONE);
        form.add(lmphone);
        form.add(mphone);

        JLabel lhphone = new JLabel(AdressBook.HOME_PHONE);
        form.add(lhphone);
        form.add(hphone);

        JLabel lpmobile = new JLabel(AdressBook.PROF_MOBILE_PHONE);
        form.add(lpmobile);
        form.add(prof_mobile_phone);

        JLabel lpoffice = new JLabel(AdressBook.PROF_OFFICE_PHONE);
        form.add(lpoffice);
        form.add(prof_office_phone);

        JLabel lhome_page = new JLabel(AdressBook.HOME_PAGE);
        form.add(lhome_page);
        form.add(home_page);

        JLabel lpemail = new JLabel(AdressBook.PROF_EMAIL);
        form.add(lpemail);
        form.add(prof_email);

        JLabel lperso_email = new JLabel(AdressBook.PERSO_EMAIL);
        form.add(lperso_email);
        form.add(perso_email);

        JLabel lskype = new JLabel(AdressBook.SKYPE);
        form.add(lskype);
        form.add(skype);

        JLabel lnotes = new JLabel(AdressBook.NOTES);
        form.add(lnotes);
        form.add(notes);

        //Display the window.
        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        //Lorsque l'on clique sur le bouton, on met à jour le JLabel
        if (arg0.getSource() == addContact) {
            test.setText(notes.getText());

            data[0] = fname.getText();
            data[1] = lname.getText();
            data[2] = haddress.getText();
            data[3] = caddress.getText();
            data[4] = mphone.getText();
            data[5] = hphone.getText();
            data[6] = prof_mobile_phone.getText();
            data[7] = prof_office_phone.getText();
            data[8] = home_page.getText();
            data[9] = prof_email.getText();
            data[10] = perso_email.getText();
            data[11] = skype.getText();
            data[12] = notes.getText();
             
            AdressBook.addContact(data);
            this.setVisible(false);
        }
        if (arg0.getSource() == cancel) {
            test.setText("cancel");
            this.setVisible(false);
        }
        if (arg0.getSource() == selectPicture) {
            int returnVal = fc.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                data[13] = file.getAbsolutePath();

                try {
                    img = ImageIO.read(file);
                    imgLabel.setIcon(new ImageIcon(img));
                    
                    
                } catch (IOException e) {
                }
            }
        }
    }
}
